// components/business/business.js
const app = getApp(), core = require('../../utils/core');
Component({

  behaviors: [],

  properties: {
    business: { // 属性名
      type: Array,
      value: []
    },
    toggleFav: {
      type: Boolean,
      value: false,
    }
  },

  data: {}, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () { },
    moved: function () { },
    detached: function () { },
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () { },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () { },
    hide: function () { },
    resize: function () { },
  },

  methods: {
    unfav: function (event) {
      let index = event.currentTarget.dataset.index;
      let company = this.data.business[index];
      core.post({
        url: '/xilujob.company/toggle_fav', data: {company_id: company.id},
        success: ret => {
          if(!ret.data.is_fav) {
            app.eventBus.emit(app.Events.CompanyFavRemove, {company: company});
          }
        }
      });
    },
  }

});
